﻿using System.Windows;
using System.Reflection;
using System.Windows.Controls;

using Builderator.Core;
using Builderator.Core.Action;

namespace Builderator.Firefly.Controls
{
    /// <summary>
    /// Interaction logic for TimeIntervalControl.xaml
    /// </summary>
    public partial class TimeIntervalControl : UserControl, IProperty
    {
        public static DependencyProperty TimeProperty = DependencyProperty.Register("Time", typeof(TimeInterval), typeof(TimeIntervalControl));

        public TimeInterval Time
        {
            get
            {
                return (TimeInterval)this.GetValue(TimeProperty);
            }
            set
            {
                this.SetValue(TimeProperty, value);

                this.Parse(value);

                if (this.OnPropertyChanged != null)
                    this.OnPropertyChanged(this.Property, value);
            }
        }

        public new event PropertyChanged OnPropertyChanged;

        private TimeInterval time;

        public PropertyInfo Property
        {
            get;
            set;
        }

        public TimeIntervalControl(PropertyInfo property)
        {
            InitializeComponent();

            this.Property = property;
        }

        private void Parse(TimeInterval value)
        {
            this.time = value;

            this.hourTextBox.Text = value.hours.ToString();

            this.minuteTextBox.Text = value.minutes.ToString();

            this.secondTextBox.Text = value.seconds.ToString();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.hourTextBox.Text != string.Empty)
                this.time.hours = int.Parse(this.hourTextBox.Text);

            if (this.minuteTextBox.Text != string.Empty)
                this.time.minutes = int.Parse(this.minuteTextBox.Text);

            if (this.secondTextBox.Text != string.Empty)
                this.time.seconds = int.Parse(this.secondTextBox.Text);

            this.Time = this.time;
        }
    }
}